package com.mm.cme.kotlin_001.level02.i_003_loop_sentence

import com.mm.cme.kotlin_001.utils.p
import com.mm.cme.kotlin_001.utils.pd

/**
 * Descriptions：循环结构
 * <p>
 * Author：ChenME
 * Date：2017/7/16
 * Email：ibelieve1210@163.com
 */
fun main(args: Array<String>) {

//    loopOfFor(args)

//    loopOfWhile()
}

private fun loopOfWhile() {
    var x = 5
    while (x > 0) {
        p("$x ")
        x--
    }

    pd()
    var y = 5
    do {
        p("$y ")
        y--
    } while (y > 0)
}

private fun loopOfFor(args: Array<String>) {
    for (arg in args) {
        p("$arg, ")
    }

    pd()
    for ((index, value) in args.withIndex()) {
        p("$index->$value, ")
    }

    pd()
    for (indexValue in args.withIndex()) {
        p("${indexValue.index}->${indexValue.value}, ")
    }

    pd()
    val list = MyIntList()
    list.add(1)
    list.add(2)
    list.add(3)
    for (i in list) {
        p("$i, ")
    }
}

class MyIterator(val iterator: Iterator<Int>) {


    operator fun next(): Int {
        return iterator.next()
    }

    operator fun hasNext(): Boolean {
        return iterator.hasNext()
    }
}

class MyIntList {

    private val list = ArrayList<Int>()

    fun add(int: Int) {
        list.add(int)
    }

    fun remove(int: Int) {
        list.remove(int)
    }

    operator fun iterator(): MyIterator {
        return MyIterator(list.iterator())
    }
}